﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.DirectoryServices;
    using System.Diagnostics;
    using System.Threading; 
    #endregion

    /// <summary>
    /// The NTDSSiteSettings object is a child of the AD Site object.
    /// </summary>
    [Serializable]
    public class NTDSSiteSettings : ADBaseObjectProperties {

        #region Members
        #region Fields
        public static readonly new string[] AttributesToGet;

        public static readonly string ObjectCategoryShortName = "nTDSSiteSettings"; 
        #endregion

        public InstanceTypeFlags InstanceType { get { return instanceType; } }
        private InstanceTypeFlags instanceType;

        public string InterSiteTopologyGenerator { get; private set; }

        public byte[] Schedule { get; private set; }

        public NTDSSiteSettingsOptionsFlags Options { get { return options; } }
        private NTDSSiteSettingsOptionsFlags options;
        #endregion

        #region Constructors
        static NTDSSiteSettings() {
            var attributesToGet = new List<string>();
            attributesToGet.AddRange(new string[]
            {
                "interSiteTopologyGenerator",
                "name",
                "options",
                "schedule"
            });

            attributesToGet.AddRange(ADBaseObjectProperties.AttributesToGet);

            AttributesToGet = attributesToGet.Distinct().ToArray();
            attributesToGet = null;
        }
        public NTDSSiteSettings() {
            this.Initialize();
        } 

        public NTDSSiteSettings(DirectoryEntry directoryEntry, bool destroyInputObject = true)
            : base(directoryEntry, destroyInputObject) {
                if (this.GetType().Name == typeof(NTDSSiteSettings).Name) {
                this.Attributes = null;
            }
        }

        public NTDSSiteSettings(SearchResult result, bool destroyInputObject = true)
            : base(result, destroyInputObject) {
                if (this.GetType().Name == typeof(NTDSSiteSettings).Name) {
                this.Attributes = null;
            }
        }

        public NTDSSiteSettings(List<string> ldifOutput)
            : base(ldifOutput) {
                if (this.GetType().Name == typeof(NTDSSiteSettings).Name) {
                this.Attributes = null;
            }
        }
        #endregion

        #region Methods
        protected override void Initialize() {
            base.Initialize();

            this.InterSiteTopologyGenerator = string.Empty;
        }
        protected override void ProcessResults() {

            try {
                base.ProcessResults();

                if (this.Attributes.ContainsKey("schedule")) {
                    this.Schedule = (byte[])this.Attributes["schedule"][0];
                }
                if (this.Attributes.ContainsKey("instanceType")) {
                    Enum.TryParse<InstanceTypeFlags>(this.Attributes["instanceType"][0].ToString(), result: out instanceType);
                }
                if (this.Attributes.ContainsKey("interSiteTopologyGenerator")) {
                    this.InterSiteTopologyGenerator = this.Attributes["interSiteTopologyGenerator"][0].ToString();
                }
                if (this.Attributes.ContainsKey("options")) {
                    Enum.TryParse<NTDSSiteSettingsOptionsFlags>(this.Attributes["options"][0].ToString(), result: out options);
                }

                this.IsValid = !string.IsNullOrWhiteSpace(this.DN)
                    && this.ObjectClass.Any(x => string.Equals(x, ObjectCategoryShortName, StringComparison.OrdinalIgnoreCase));
            }
            catch (Exception e) {
                Debug.WriteLine(string.Format("{0}  [ThreadID: {1}]  {2} Error: {3}", DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId, CustomExtensions.CurrentMethodName(), e.VerboseExceptionString()));
                throw;
            }
        }
        [DebuggerStepThroughAttribute]
        public override string ToString() {
            string details = this.DN;
            if (string.IsNullOrWhiteSpace(details)) details = "N/A";
            return details;
        } 
        #endregion
    }
}
